package com.weibo;

import java.awt.event.InputEvent;

import chrriis.dj.nativeswing.swtimpl.components.WebBrowserAdapter;
import chrriis.dj.nativeswing.swtimpl.components.WebBrowserEvent;
import chrriis.dj.nativeswing.swtimpl.components.WebBrowserNavigationEvent;

public class WeiBoCommentListener extends WebBrowserAdapter{
	
	/**
	 * 地址栏变化
	 */
	public void locationChanged(WebBrowserNavigationEvent e) {
		
		System.out.println( "locationChanged" );
		String newResourceLocation = e.getNewResourceLocation();
		if ( newResourceLocation.startsWith("http://weibo.com/u/") ) {
			e.consume();
			SinaWeibo.navigate("http://weibo.com/1401780831/zeSK0xMii");
		}
	}
	
	/**
	 * 监听页面是否加载完毕
	 */
	public void loadingProgressChanged( WebBrowserEvent e ) {
		
		String url = e.getWebBrowser().getResourceLocation();
		if( !url.startsWith("http://weibo.com/u/")){
			return;
		}
		System.out.println( "loadingProgressChanged" );
		
		if (e.getWebBrowser().getLoadingProgress() == 100 ) {
			
			SinaWeibo.REBOT.mouseMove(1260,572); // 移动

			for (int i = 0; i < 3; i++) {
				SinaWeibo.REBOT.delay(1000);
				SinaWeibo.REBOT.mousePress(InputEvent.BUTTON1_MASK); // 按下鼠标左键
				SinaWeibo.REBOT.mouseRelease(InputEvent.BUTTON1_MASK); // 释放鼠标左键
			}
		
			String script = " document.getElementsByTagName('textarea')[0].value='真的有那么好用吗？';";
			e.getWebBrowser().executeJavascript(script);
			
			try {
				Thread.sleep( 500 );
			} catch (InterruptedException e1) {
				e1.printStackTrace();
			}

//			
//			//开始评论
//			SinaWeibo.REBOT.mouseMove(675,509); // 移动
//			SinaWeibo.REBOT.mousePress(InputEvent.BUTTON1_MASK); // 按下鼠标左键
//			SinaWeibo.REBOT.mouseRelease(InputEvent.BUTTON1_MASK); // 释放鼠标左键
			
			SinaWeibo.removeListener( this );
		}
	}
	
	
}
